#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_login import LoginManager
from flask_restful import Api

app = Flask(__name__)
# 配置数据库地址
app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite:///tmp/product-demo.db"
# 动态追踪修改设置，如未设置只会提示警告
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = True
# 全局取消CSRF验证
app.config['WTF_CSRF_ENABLED'] = False
# 创建一个处理所有ORM活动的对象 实例化需要写在配置数据库之后
db = SQLAlchemy(app)
# 绑定app和数据库
migrate = Migrate(app, db)

api = Api(app)  # 创建api对象，使用flask程序初始化  rest ful api

# 文件配置
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'JPG', 'PNG', 'bmp'])
app.config['UPLOAD_FOLDER'] = os.path.relpath('.') + '/static/uploads'
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'auth.login'

# 注册试图
from my_app.auth.views import auth
from my_app.catalog.views import catalog
from my_app.cash_value.views import cash_value
from my_app.create_photo.views import create_photo
from my_app.record_policy.views import record_policy
from my_app.modify_db.views import modify_db
from my_app.product_shelves.views import product_shelves
app.register_blueprint(auth)
app.register_blueprint(catalog)
app.register_blueprint(cash_value)
app.register_blueprint(create_photo)
app.register_blueprint(record_policy)
app.register_blueprint(modify_db)
app.register_blueprint(product_shelves)
app.secret_key = "some_random_key"  # 暂时弃用

# 生成数据表
with app.app_context():
    db.create_all()

